﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StorageApp.APIS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;
using ZXing;

namespace StorageApp.Utility
{
    class HttpWebUtility
    {
         
        public  async static Task<JObject> CreateGetHttpResponse(string url)
        {
            using (var client = newHttpService())
            {
                try
                {
                    // 发送GET请求
                    var response = await client.GetAsync(Method.IniReadValue("url")+url);

                    return await responseData(response);
                 
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                    return new JObject();

                }
            }
        }
        public static async Task<JObject>  responseData(HttpResponseMessage response)
        {
            string msg = "";
            if (response.IsSuccessStatusCode )
            {
                // 读取响应内容
                var json = await response.Content.ReadAsStringAsync();
                var res= JObject.Parse(json);
                if (res["code"].ToString()== "200")
                {
                    return res;
                }
                else
                {
                    msg = res["message"].ToString();
                }

            }
            if (msg == "")
            {
                msg = response.RequestMessage.ToString();

            }
            System.Windows.Forms.MessageBox.Show(msg);
            return new JObject();
             
        }
    public static HttpClient newHttpService()
        {
            var client= new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", Method.IniReadValue("Authorization"));
            return client;
        }
        /// <summary>  
        /// 创建POST方式的HTTP请求  
        /// </summary>  
        /// <param name="url">请求的URL</param>  
        /// <param name="parameters">随同请求POST的参数名称及参数值字典</param>  
        /// <param name="timeout">请求的超时时间</param>  
        /// <param name="userAgent">请求的客户端浏览器信息，可以为空</param>  
        /// <param name="requestEncoding">发送HTTP请求时所用的编码</param>  
        /// <param name="cookies">随同HTTP请求发送的Cookie信息，如果不需要身份验证可以为空</param>  
        /// <returns></returns>  
        public async static Task<JObject> CreatePostHttpResponse(string url, IDictionary<string, string> parameters)
        {
            using (var client = newHttpService())
            {
                try
                {
                    var httpContent = new FormUrlEncodedContent(parameters);
                    // 发送GET请求
                    var response = await client.PostAsync(Method.IniReadValue("url") + url, httpContent);

                    return await responseData(response);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                    return new JObject();

                }
            }
        }

    }
}
